Skip to content

fix(quantization): accept QuantizeAlgorithmConfig in get_modelike_from_algo_cfg#1514

Open
javierdejesusda wants to merge 1 commit into
NVIDIA:mainfrom
javierdejesusda:fix/get-modelike-accept-algo-config-201
Open

fix(quantization): accept QuantizeAlgorithmConfig in get_modelike_from_algo_cfg#1514
javierdejesusda wants to merge 1 commit into
NVIDIA:mainfrom
javierdejesusda:fix/get-modelike-accept-algo-config-201

Conversation

@javierdejesusda
Copy link
Copy Markdown
Contributor

@javierdejesusda javierdejesusda commented May 18, 2026

What does this PR do?

Type of change: Bug fix

Fixes #201 - get_modelike_from_algo_cfg rejected QuantizeAlgorithmConfig instances with ValueError, despite the algorithm field's type annotation allowing them.

Root cause: QuantizeAlgoCfgType (modelopt/torch/quantization/config.py) explicitly includes QuantizeAlgorithmConfig, and QuantizeConfig.algorithm is typed with it. But get_modelike_from_algo_cfg only matched list / None / str / dict and fell through to raise ValueError(f"Invalid config type: {type(algo_cfg)}") for a config instance:

ValueError: Invalid config type: <class 'modelopt.torch.quantization.config.MaxCalibConfig'>

Fix: ModeloptBaseConfig is a collections.abc.MutableMapping, so the type cascade's dict check is widened to Mapping, and the matched config is materialized with dict(...) so the resolved config keeps its ConfigDict (dict[str, Any]) type:

elif isinstance(algo_cfg, Mapping):
    # Normalize any mapping (incl. ModeloptBaseConfig instances) to a plain dict.
    algo_cfg = dict(algo_cfg)
    algo_name = algo_cfg["method"]

The change is a strict no-op for the previously supported str / dict / None / list inputs (a plain dict is a Mapping); only the path that previously raised changes.

Usage

import modelopt.torch.quantization as mtq

cfg = mtq.INT8_DEFAULT_CFG
cfg["algorithm"] = mtq.MaxCalibConfig(method="max", distributed_sync=False)

# Previously: ValueError: Invalid config type: <class '...MaxCalibConfig'>
# Now accepted, consistent with the `algorithm` field's type annotation.
mtq.quantize(model, cfg, forward_loop)

Testing

Added two regression tests:

  • tests/unit/torch/quantization/test_mode.py::test_get_modelike_from_algo_cfg_accepts_config_instance - a single config instance and a list of config instances (resolved mode names, dict round-trip).
  • tests/unit/torch/quantization/test_quantize_cpu.py::test_quantize_accepts_algo_config_instance_end_to_end - the issue's repro through the full mtq.quantize / calibrate flow.
pytest tests/unit/torch/quantization/test_mode.py::test_get_modelike_from_algo_cfg_accepts_config_instance \
       tests/unit/torch/quantization/test_quantize_cpu.py::test_quantize_accepts_algo_config_instance_end_to_end

Without the fix both tests fail with the exact ValueError: Invalid config type; with it they pass. The full test_mode.py and test_quantize_cpu.py suites pass locally, and ruff check / ruff format --check are clean on the changed files.

Before your PR is "Ready for review"

Make sure you read and follow the Contributor guidelines and your commits are signed (git commit -s -S).

Make sure you read and follow the Security Best Practices for contributors.

  • Is this change backward compatible?: ✅
  • If you copied code from any other sources or added a new PIP dependency, did you follow guidance in CONTRIBUTING.md: N/A
  • Did you write any new necessary tests?: ✅
  • Did you update Changelog?: ✅
  • Did you get Claude approval on this PR?: N/A

Additional Information

Closes #201. Bug confirmed by @realAsma on the issue thread. The fix uses the Mapping-based approach suggested by @shengliangxu in review (ModeloptBaseConfig became a MutableMapping in #1405). Change is +41/-4 across modelopt/torch/quantization/mode.py, the two test files, and CHANGELOG.rst.

Summary by CodeRabbit

  • Bug Fixes

    • Quantization now accepts algorithm configuration objects as the algorithm parameter instead of rejecting them with an error.
  • Tests

    • Added regression and end-to-end tests to verify algorithm config instances are accepted and preserved through the quantization workflow.
  • Documentation

    • Changelog updated to record the bug fix in version 0.45.

Review Change Stack

@javierdejesusda javierdejesusda requested a review from a team as a code owner May 18, 2026 15:47
@copy-pr-bot
Copy link
Copy Markdown

copy-pr-bot Bot commented May 18, 2026

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 18, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 5b8c41f1-6b1b-41ca-9dd4-ee9672c7099e

📥 Commits

Reviewing files that changed from the base of the PR and between a474a71 and 7b5e4c8.

📒 Files selected for processing (4)
  • CHANGELOG.rst
  • modelopt/torch/quantization/mode.py
  • tests/unit/torch/quantization/test_mode.py
  • tests/unit/torch/quantization/test_quantize_cpu.py
✅ Files skipped from review due to trivial changes (1)
  • CHANGELOG.rst

📝 Walkthrough

Walkthrough

The PR normalizes algorithm-config inputs that are Mappings or QuantizeAlgorithmConfig instances to plain dicts in get_modelike_from_algo_cfg, adds a v0.45 bug-fix note, and includes unit and end-to-end tests validating acceptance of config instances.

Changes

QuantizeAlgorithmConfig instance acceptance

Layer / File(s) Summary
Detect and normalize Mapping/Config inputs
modelopt/torch/quantization/mode.py
get_modelike_from_algo_cfg treats Mapping inputs as config-like and normalizes them via dict(algo_cfg) before extracting method/algo_name.
Release note
CHANGELOG.rst
Adds v0.45 Bug Fixes entry stating QuantizeAlgorithmConfig instances are accepted for the algorithm field.
Unit tests for get_modelike_from_algo_cfg
tests/unit/torch/quantization/test_mode.py
Adds a regression unit test passing QuantizeAlgorithmConfig instances (and lists) to get_modelike_from_algo_cfg, asserting returned mode names and that algorithm configs are normalized to plain dicts preserving fields.
End-to-end quantize workflow test
tests/unit/torch/quantization/test_quantize_cpu.py
Adds integration test that sets config["algorithm"] to a MaxCalibConfig instance and runs quantize/calibration on a sample model to ensure the workflow completes without raising.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 5 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: enabling get_modelike_from_algo_cfg to accept QuantizeAlgorithmConfig instances, matching the core objective.
Linked Issues check ✅ Passed The PR successfully addresses issue #201 by modifying get_modelike_from_algo_cfg to accept QuantizeAlgorithmConfig instances as Mapping types and convert them to dicts, with comprehensive unit and end-to-end regression tests.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the QuantizeAlgorithmConfig handling: core fix in mode.py, targeted regression tests, and changelog update documenting the bug fix.
Security Anti-Patterns ✅ Passed No security anti-patterns found. Changes only normalize config Mapping instances to dicts without any unsafe deserialization, code execution, or hardcoded security flags.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

Comment thread modelopt/torch/quantization/mode.py Outdated
algo_cfg = algo_cfg.model_dump()
if algo_cfg is None or isinstance(algo_cfg, str):
algo_name, algo_cfg = algo_cfg, {}
elif isinstance(algo_cfg, dict):
Copy link
Copy Markdown
Collaborator

@shengliangxu shengliangxu May 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for helping fix this bug. Let's instead change this to

Suggested change
elif isinstance(algo_cfg, dict):
elif isinstance(algo_cfg, Mapping):

QuantizeAlgorithmConfig is a MutableMapping.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review, and good call. I'll rebase onto current main.

One follow-on: with that change the resolved config is passed through as a Mapping rather than a fresh dict, so I'll update the isinstance(..., dict) assertion in the new unit test to match.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I was thinking of using Mapping so we can get rid of the dict conversion, but, I guess need more changes across the context. Nevermind.

pass


def test_get_modelike_from_algo_cfg_accepts_config_instance():
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests look good. Thanks for adding them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'll keep them in the revised version, with the dict assertion updated to reflect the Mapping pass-through.

…m_algo_cfg

get_modelike_from_algo_cfg raised ValueError for a QuantizeAlgorithmConfig instance even though QuantizeAlgoCfgType lists it as a valid type. Since ModeloptBaseConfig is a Mapping, widen the type cascade's dict check to Mapping and materialize the resolved config with dict(). Add unit and end-to-end regression tests plus a CHANGELOG entry. Fixes NVIDIA#201

Signed-off-by: javierdejesusda <javier.dejesusj9@gmail.com>
@javierdejesusda javierdejesusda force-pushed the fix/get-modelike-accept-algo-config-201 branch from a474a71 to 7b5e4c8 Compare May 20, 2026 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

get_modelike_from_algo_cfg doesn't accept QuantizeAlgorithmConfig, but it's typing says it should

2 participants